package com.fdsc.erp.datasource;

import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;


import java.util.Properties;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    @Bean("taDataSource")
    @Primary
    @ConfigurationProperties("datasource.tadata")

    public DataSource taDataSource() {
        return DataSourceBuilder.create().build();
    }
//    @Bean("operateDataSource")
//    @ConfigurationProperties(prefix = "datasource.operatedata")
//    public DataSource operateDataSource() {
//        return DataSourceBuilder.create().build();
//    }


    @Bean("taSqlSessionFactory")
    @Primary
    public SqlSessionFactory taSqlSessionFactory(@Autowired @Qualifier("taDataSource") DataSource dataSource) throws Exception {

        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/ta/*.xml"));
/*
        PaginationInterceptor interceptor = new PaginationInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "sqlserver");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new PaginationInterceptor[] {interceptor});*/
        //加载分页插件
        PaginationInterceptor page = new PaginationInterceptor();
        sqlSessionFactoryBean.setPlugins(new PaginationInterceptor[]{page});
        //return page;
        return sqlSessionFactoryBean.getObject();

    }
    /**
     * 分页插件
     */
   /* @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType(DBType.SQLSERVER.getDb());
        return page;
        //return new PaginationInterceptor();
    }*/
/*
    @Bean("operateCenterSqlSessionFactory")
    public SqlSessionFactory operateCenterSqlSessionFactory(@Autowired @Qualifier("operateDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/operate/*.xml"));
        
//        Interceptor interceptor = new PageInterceptor();
//        Properties properties = new Properties();
//        properties.setProperty("helperDialect", "mysql");
//        properties.setProperty("offsetAsPageNum", "true");
//        properties.setProperty("rowBoundsWithCount", "true");
//        properties.setProperty("reasonable", "false");
//        interceptor.setProperties(properties);
//        sqlSessionFactoryBean.setPlugins(new Interceptor[] {interceptor});

        return sqlSessionFactoryBean.getObject();
    }*/

}
